package com.oliet.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oliet.common.dto.dict.CreateDictionaryDTO;
import com.oliet.common.dto.dict.UpdateDictionaryDTO;
import com.oliet.common.vo.dict.MainDictionaryVO;
import com.oliet.common.vo.page.PageVO;
import com.oliet.common.vo.role.RoleVO;
import com.oliet.entity.Dictionary;
import com.oliet.entity.Roles;
import com.oliet.entity.base.BaseEntity;
import com.oliet.exception.CustomException;
import com.oliet.exception.ErrorEnum;
import com.oliet.mapper.DictionaryMapper;
import com.oliet.service.DictionaryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author JZP
 * @since 2023-12-30
 */
@Service
public class DictionaryServiceImpl extends ServiceImpl<DictionaryMapper, Dictionary> implements DictionaryService {

    @Override
    public List<MainDictionaryVO> getAll() {
        List<Dictionary> poList = this.list();
        return MainDictionaryVO.poListConverGetAllVoList(poList);
    }

    @Override
    public void create(CreateDictionaryDTO param) {
        long count = this.count(new LambdaQueryWrapper<Dictionary>()
                .eq(Dictionary::getMainCode, param.getMainCode())
                .eq(Dictionary::getItemCode, param.getItemCode()));
        if (count > 0) {
            throw new CustomException(ErrorEnum.DICTIONARY_EXIST);
        }
        Dictionary po = Dictionary.builder()
                .mainCode(param.getMainCode())
                .mainName(param.getMainName())
                .itemCode(param.getItemCode())
                .itemName(param.getItemName())
                .description(param.getDescription())
                .build();
        this.save(po);
    }

    @Override
    public void update(UpdateDictionaryDTO param) {
        Dictionary po = this.getById(param.getId());
        if (ObjectUtil.isNull(po)) {
            throw new CustomException(ErrorEnum.DATA_DOES_NOT_EXIST);
        }
        po.setMainName(param.getMainName());
        po.setItemName(param.getItemName());
        po.setDescription(param.getDescription());
        this.updateById(po);
    }

    @Override
    public PageVO<Dictionary> pageList(Long pageNo, Long pageSize, String search) {
        //分页查询
        Page<Dictionary> page = new Page<>(pageNo, pageSize);

        Page<Dictionary> poData = this.page(page, new LambdaQueryWrapper<Dictionary>()
                .and(
                        StrUtil.isNotBlank(search),
                        wrapper -> wrapper.like(Dictionary::getMainCode, search)
                                .or()
                                .like(Dictionary::getMainCode, search)
                                .or()
                                .like(Dictionary::getItemCode, search)
                                .or()
                                .like(Dictionary::getItemName, search)
                                .or()
                                .like(Dictionary::getDescription, search)
                ).orderByDesc(BaseEntity::getDateCreated, Dictionary::getItemCode)
        );
        PageVO<Dictionary> pageVo = new PageVO<>(poData.getCurrent(), poData.getSize(), poData.getTotal(), poData.getRecords());
        return pageVo;
    }
}
